﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace 第三次作业
{
    internal class statistics
    {
        public int lineCount=0;
        public int wordCount = 0;

        public string location { get; set; }

        public void dostatistic()
        {
            FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);

            StreamReader sr = new StreamReader(fs,System.Text.Encoding.UTF8);

            int count = 0;

            for(string s = sr.ReadLine(); s != null; s = sr.ReadLine())
            {
                count++;
                wordCount += Regex.Matches(s, @"([a-z]|[A-Z]|[0-9])+").Count() ;
            }
            lineCount = count;
        }

        public Dictionary<string,int> dostatisticpro()
        {
            FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);

            StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);

            Dictionary<string, int> dic = new Dictionary<string, int>();

            bool flag = false;

            for (string s = sr.ReadLine(); s != null; s = sr.ReadLine())
            {
                foreach (Match match in Regex.Matches(s, @"([a-z]|[A-Z]|[0-9])+"))
                {
                    foreach (string keys in dic.Keys)
                    {
                        if (keys == match.Value) { 
                            dic[keys] += 1;
                            flag = true;
                            break;
                        }
                    }
                    if(flag == false)
                    {
                        dic.Add(match.Value, 1);
                    }
                    else { flag = false; }
                    
                }
            }
            return dic;
        }

    }
}
